﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Data;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;
namespace WebApplication
{
    public partial class BrandHTML : System.Web.UI.Page
    {
        int customeruid = 0;
        int branduid = 0;
        tblCustomer customerENT = new tblCustomer();
        tblCustomerBLL customerBLL = new tblCustomerBLL();
        mtblBrand brandENT = new mtblBrand();
        mtblBrandBLL brandBLL = new mtblBrandBLL();

        tblBrand_ProdCat brand_prodCatENT = new tblBrand_ProdCat();
        tblBrand_ProdCatBLL brand_prodCatBLL = new tblBrand_ProdCatBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString[Constant.UrlParamName.CustomerUID] != null)
                {
                    if (int.TryParse(Request.QueryString[Constant.UrlParamName.CustomerUID], out customeruid))
                    {
                        InitPage();
                    }
                    else
                    {
                        SetNoOption();
                        lblMeg.Text = ResourceManage.GetResourceString("msg200001");
                    }
                }
                else
                {
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200002");
                }
            }
        }
        private void InitPage()
        {
            customerENT.CustomerUID = customeruid;
            customerENT = customerBLL.GetEntity(customerENT);
            if (customerENT != null)
            {
                BindProductCat();
                hidCustomerUID.Value = customerENT.CustomerUID.ToString();

                if (Request.QueryString[Constant.UrlParamName.BrandUID] != null)
                {
                    if (int.TryParse(Request.QueryString[Constant.UrlParamName.BrandUID], out branduid))
                    {
                        brandENT.BrandUID = branduid;
                        brandENT.CustomerUID = customerENT.CustomerUID;

                        brandENT = brandBLL.GetEntity(brandENT);
                        if (brandENT != null)
                        {
                            hidBrandUID.Value = brandENT.BrandUID.ToString();
                            brand_prodCatENT.BrandUID = brandENT.BrandUID;
                            var list = brand_prodCatBLL.GetListByENT(brand_prodCatENT);
                            {

                            }


                        }
                        else
                        {
                            SetNoOption();
                            lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                        }
                    }
                }
            }
            else
            {
                SetNoOption();
                lblMeg.Text = ResourceManage.GetResourceString("msg200014");
            }
        }

        private void SetNoOption()
        {
            lkBtnSave.Enabled = false;
            hidAllowOption.Value = "false";
        }

        #region 绑定Cat
        private void BindProductCat()
        {
            List<string> strlist = new List<string>();
            strlist.Add("A");
            strlist.Add("B");
            strlist.Add("C");
            strlist.Add("D");
            strlist.Add("E");
            DataTable dt = new DataTable();
            DataColumn col = new DataColumn();
            col.ColumnName = "startwith";
            dt.Columns.Add(col);
            foreach (var temp in strlist)
            {
                DataRow dr = dt.NewRow();
                dr[0] = temp;
                dt.Rows.Add(dr);
            }
            rptStart.DataSource = dt;
            rptStart.DataBind();
        }

        protected void rptStart_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
                string ProductCatID = (string)((DataRowView)e.Item.DataItem)["startwith"];
                Repeater rptCat = e.Item.FindControl("rptCat") as Repeater;
                Repeater rptCatForSubCat = e.Item.FindControl("rptCatForSubCat") as Repeater;
                if (rptCat != null)
                {
                    
                    mtblProductCat catENT = new mtblProductCat();
                    mtblProductCatBLL catBLL = new mtblProductCatBLL();
                    catENT.ProductCatID = ProductCatID;
                    catENT.Deleted = 0;
                    var list = catBLL.GetListByENT(catENT);
                    rptCat.DataSource = list;
                    rptCat.DataBind();
                    rptCatForSubCat.DataSource = list;
                    rptCatForSubCat.DataBind();
                }
        }

        protected void rptCatForSubCat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Repeater rptSubCat = e.Item.FindControl("rptSubCat") as Repeater;
                if (rptSubCat != null)
                {
                    mtblProductCat catENT = new mtblProductCat();
                    catENT = e.Item.DataItem as mtblProductCat;
                    mtblProductSubCat subCatENT = new mtblProductSubCat();
                    mtblProductSubCatBLL subCatBLL = new mtblProductSubCatBLL();
                    subCatENT.ProductCatUID = catENT.ProductCatUID;
                    subCatENT.Deleted = 0;
                    var list = subCatBLL.GetListByENT(subCatENT);
                    rptSubCat.DataSource = list;
                    rptSubCat.DataBind();
                }
            }
        }
        #endregion



        #region by jimmy
        ///// <summary>
        ///// 绑定 五大类别下的大类别
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void rptCatAll_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        string productCatID = e.Item.DataItem as string;
        //        mtblProductCat catENT = new mtblProductCat();
        //        mtblProductCatBLL catBLL = new mtblProductCatBLL();
        //        catENT.ProductCatID = productCatID;
        //        var list = catBLL.GetListByENT(catENT);
        //        CheckBoxList ckbCatList = e.Item.FindControl("ckbCatList") as CheckBoxList;
        //        ckbCatList.DataSource = list;
        //        ckbCatList.DataTextField = "ProductCat_Chi";
        //        ckbCatList.DataValueField ="ProductCatUID";
        //        ckbCatList.DataBind();

        //        Repeater rptSubCat = e.Item.FindControl("rptCat") as Repeater;
        //        rptSubCat.DataSource = list;
        //        rptSubCat.DataBind();
        //    }
        //}

        //protected void rptCat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        mtblProductCat catENT = new mtblProductCat();
        //        catENT = e.Item.DataItem as mtblProductCat;
        //        mtblProductSubCat subCatENT = new mtblProductSubCat();
        //        mtblProductSubCatBLL subCatBLL = new mtblProductSubCatBLL();
        //        subCatENT.ProductCatUID = catENT.ProductCatUID;
        //        var list = subCatBLL.GetListByENT(subCatENT);

        //        CheckBoxList ckbSubCatList = e.Item.FindControl("ckbSubCatList") as CheckBoxList;
        //        ckbSubCatList.DataSource = list; 
        //        ckbSubCatList.DataTextField = "ProductSubCat_Chi";
        //        ckbSubCatList.DataValueField = "ProductSubCatUID";
        //        ckbSubCatList.DataBind();
        //    }
        //}
        #endregion
    }
}